// World.java
//
// Contains all the objects that are actually in the game world. All the tiles 
// and items and mobs and stuff

import java.util.Scanner;
import java.util.ArrayList;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

public class World
{
    private Tile[][] landscape;
    private ArrayList<Mob> moveQueue;

    // Displays all of the tiles in the 'World'
    public void display(Graphics g)
    {
        for (int i = 0; i < landscape.length; i ++)
        {
            for (int j = 0; j < landscape[i].length; j++)
            {
                landscape[i][j].display(j * 24 + 8, i * 24 + 30, g);
            }
        }
    }

    // Generates a new world with the size of x, y
    public void generate(int x, int y)
    {
        landscape = new Tile[y][x];
        for (int ty = 0; ty < landscape.length; ty ++)
        {
            for (int tx = 0; tx < landscape[ty].length; tx++)
            {
                landscape[ty][tx] = new Tile(1, this, tx, ty);
            }
        }
        
        landscape[8][7].setTerrain(2);
        landscape[9][7].setTerrain(2);
        landscape[10][7].setTerrain(2);
        landscape[11][7].setTerrain(2);
        landscape[11][8].setTerrain(2);
        landscape[11][9].setTerrain(2);
	landscape[11][10].setTerrain(2);
        landscape[0][0].setTerrain(2);
        landscape[22][29].setTerrain(2);
       	landscape[10][8].addNewItem(1);
	landscape[13][15].addNewMob(1);
        landscape[13][15].getMob(0).equip(new Item(1));

        moveQueue = new ArrayList<Mob>();
    }

    // The ever-present 'tick' method. Basically this just makes everything in the 
    // World 'tick' as it were. That just means that every item, terrain, and mob 
    // perform their tick methods, which is usually advancing forward in time in some way
    // eg A mob's tick method will make it move in a direction
    public void tick()
    {
        for (int i = 0; i < landscape.length; i++)
        {
            for (int j = 0; j < landscape[i].length; j++)
            {
                landscape[i][j].tick();
            }
        }
        move();
	clearQueue();
    }

    // Move Queue Methods
    // This is how movements work: On a mob's tick method, the mob adds itself to 
    // the move queue, which is basically like saying 'yo, I wanna move!'. After 
    // the tick methods have all ocurred, the move methods are called for all of 
    // the mobs in move queue. This is to avoid really annoying errors caused
    // by iterating through the landscape array and modifying it at the same time!
    public void queue(Mob m)
    {
        moveQueue.add(m);
    }

    public void move()
    {
        for (Mob m : moveQueue)
        {
            m.move();
        }
    }

    public void clearQueue()
    {
        moveQueue.clear();
    }
    
    // checks to see if a Tile is valid, based on an x-y coordinate
    public boolean isValid(int x, int y)
    {
        try
        {
            landscape[y][x].getX();
            landscape[y][x].getY();
            return true;
        }
        catch(IndexOutOfBoundsException e)
        {
            return false;
        }
    }

    // GETS AND SETS
    public Tile[][] getLandscape()
    {
        return landscape;
    }

}